﻿from flask import Flask
from flask import request
from flask import render_template  # 导入其实的网页
from flask import jsonify
from jieba.analyse import extract_tags
import string
import utils
import json
import Utils  # 调用 自定义的时间格式

app = Flask(__name__)


@app.route('/')
def hello_world():  # 输入一个http://127.0.0.1:5000/就返回hello world
    return render_template("main.html")  # 默认返回这个页面


@app.route('/c1')
def get_c1_data01():
    data = Utils.get_cl_data()
    dict1 = []
    for i in data:
        dict1.append(str(i))
    data = tuple(dict1)
    datas = {"confirm": data[0], "suspect": data[1], "heal": data[2], "dead": data[3]}
    s = json.dumps(datas)  # 把decimal先转换为字符串，在转换为字符串，在转换成js（直接Decimal转成js会报错）
    return json.loads(s)


@app.route("/c2")
def get_c2_data02():
    res = []
    for tup in Utils.get_c2_data():
        res.append({"name": str(tup[0]), "value": int(tup[1])})
    return {"data": res}


@app.route("/l1")
def get_l1_data03():
    data = Utils.get_l1_data()
    day, confirm, suspect, heal, dead = [], [], [], [], []
    for a, b, c, d, e in data:
        day.append(a.strftime("%m-%d"))  # a是datatime类型
        confirm.append(b)
        suspect.append(c)
        heal.append(d)
        dead.append(e)
    return {"day": day, "confirm": confirm, "suspect": suspect, "heal": heal, "dead": dead}


@app.route("/l2")
def get_l2_data04():
    data = Utils.get_l2_data()
    day, confirm, suspect = [], [], []
    for a, b, c in data:
        day.append(a.strftime("%m-%d"))  # a是datatime类型
        confirm.append(str(b))
        suspect.append(str(c))
    return {"day": day, "confirm": confirm, "suspect": suspect}


@app.route("/r1")
def get_r1_data():
    data = Utils.get_r1_data()
    city = []
    confirm = []
    for k, v in data:
        city.append(str(k))
        confirm.append(int(v))
    return {"city": city, "confirm": confirm}


@app.route("/r2")
def get_r2_data():
    data = Utils.get_r2_data()  # 格式 (('民警抗疫一线奋战16天牺牲1037364',), ('四川再派两批医疗队1537382',)
    d = []
    for i in data:
        k = i[0].rstrip(string.digits)  # 移除热搜数字
        v = i[0][len(k):]  # 获取热搜数字
        ks = extract_tags(k)  # 使用jieba 提取关键字
        for j in ks:
            if not j.isdigit():
                d.append({"name": j, "value": v})
    return {"kws": d}


@app.route('/time')  # 返回时间
def gettime():
    return Utils.get_time()



@app.route('/ajax', methods=["get", "post"])  # 让get,post请求都可以
def hellow_world04():
    name = request.values.get("name")
    scort = request.values.get("scort")
    print(name + str(scort))
    return "10000"


@app.route('/tem')  # http://127.0.0.1:5000/tem :就出现新的网页
def hello_world03():
    return render_template("index.html")  # 添加 网页


@app.route('/login')
def hellow_world02():  # 输入密码和id了点击提交，会返回结果
    name = request.values.get("name")
    pwd = request.values.get("pwd")
    return f'name={name},pwd={pwd}'


@app.route('/abc')
def hellow_world01():  # 输入http://127.0.0.1:5000/abc 就返回from表单
    id = request.values.get("id")
    return f"""
    <form action="/login">
    账号：<input name = "name" values="{id}"> <br>
    密码：<input name = "pwd" >
         <input type="submit">
    </form>
    """


if __name__ == '__main__':
    app.run(host="0.0.0.0",port=5000)
